# from pyhanlp import *

# # 示例学生数据
# students = [
#     {'name': 'Alice', 'grade': 95},
#     {'name': 'Bob', 'grade': 90},
#     {'name': 'Charlie', 'grade': 92},
#     {'name': 'David', 'grade': 88},
#     {'name': 'Eva', 'grade': 97}
# ]

# # 初始化HanLP分词器
# HanLP = JClass('com.hankcs.hanlp.HanLP')

# # 处理查询函数
# def process_query(query):
#     # 分词
#     words = HanLP.segment(query)
#     intent = None
#     top_n = None
#     keywords=[]
#     # if 'best' in keywords or 'top' in keywords:
#     # print("words")
#     # for word in words:
#     #     print("====")
#     #     print(word)
#     for word_type in words:
#         word_type_parts=word_type.split("/")
#         word=word_type_parts[0]
#         type_name=word_type_parts[1]
#         # print("====")
#         # print(word)
#         keywords.append(word)
#         if 'best' in word or 'top' in word:
#             intent = 'best'
#     # for word,type_name in words:
#     #     keywords.append(word)
#     #     if 'best' in word or 'top' in word:
#     #         intent = 'best'
#     for i in range(len(keywords)):
#         if keywords[i] == 'top' and i < len(keywords) - 1:
#             try:
#                 top_n = int(keywords[i + 1])
#                 break
#             except ValueError:
#                 pass
#     # keywords = [str(word) for word in words]
#     # print("keywords")
#     # print(keywords)
    
#     # 解析查询意图和top_n参数
    
#     # if 'best' in keywords or 'top' in keywords:
#     #     intent = 'best'
#     #     for i in range(len(keywords)):
#     #         if keywords[i] == 'top' and i < len(keywords) - 1:
#     #             try:
#     #                 top_n = int(keywords[i + 1])
#     #                 break
#     #             except ValueError:
#     #                 pass
    
#     return intent, top_n

# # 查询处理函数
# def process_query_results(intent, students, top_n=3):
#     if intent == 'best':
#         # 根据成绩排序学生
#         sorted_students = sorted(students, key=lambda s: s['grade'], reverse=True)
        
#         # 输出前几名学生
#         for i in range(min(top_n, len(sorted_students))):
#             student = sorted_students[i]
#             print(f"Name: {student['name']}, Grade: {student['grade']}")
#     else:
#         print("Sorry, I couldn't understand your query.")

# # 示例查询
# query = "Who are the top 2 students in the school?"

# # 处理查询
# intent, top_n = process_query(query)

# # 处理查询结果
# if top_n is not None:
#     process_query_results(intent, students, top_n)
# else:
#     print("Sorry, I couldn't understand your query or detect the top_n parameter.")



from pyhanlp import *

# 示例学生数据
students = [
    {'name': 'Alice', 'grade': 95},
    {'name': 'Bob', 'grade': 90},
    {'name': 'Charlie', 'grade': 92},
    {'name': 'David', 'grade': 88},
    {'name': 'Eva', 'grade': 97}
]

# 初始化HanLP分词器和词性标注器
HanLP = JClass('com.hankcs.hanlp.HanLP')
Nature = JClass('com.hankcs.hanlp.corpus.tag.Nature')

# 处理查询函数
def process_query(query):
    # 分词和词性标注
    words = HanLP.segment(query)
    keywords = [(str(word.word), str(word.nature)) for word in words]
    print("keywords")
    print(keywords)
    
    # 解析查询意图和top_n参数
    intent = None
    top_n = None
    #  ('top', 'nx'), (' ', 'w'), ('2', 'm'), 
    # print
    nx='nx'
    # if ('best', 'a') in keywords or ('top', 'm') i
    # ('最好', 'd')
    best_tuple=('最好', 'd')
    best_tuple_eng=('top', 'nx')
    if ('best', 'a') in keywords or best_tuple in keywords:
        intent = 'best'
        for i in range(len(keywords)):
            keyword=keywords[i]
            # print(keywords)
            print("keyword")
            print(keyword)
            # ] == ('top', 'm')
            if keywords[i] == best_tuple and i < len(keywords) - 1:
                try:
                    # top_n = int(keywords[i + 1][0])
                    top_n = int(keywords[i + 2][0])
                    break
                except ValueError:
                    pass
    
    return intent, top_n

# 查询处理函数
def process_query_results(intent, students, top_n=3):
    if intent == 'best':
        # 根据成绩排序学生
        sorted_students = sorted(students, key=lambda s: s['grade'], reverse=True)
        
        # 输出前几名学生
        for i in range(min(top_n, len(sorted_students))):
            student = sorted_students[i]
            print(f"Name: {student['name']}, Grade: {student['grade']}")
    else:
        print("Sorry, I couldn't understand your query.")

# 示例查询
# query = "Who are the top 2 students in the school?"
query = "成绩最好的2个人"

# 处理查询
intent, top_n = process_query(query)
print("query")
print(query)

# 处理查询结果
if top_n is not None:
    process_query_results(intent, students, top_n)
else:
    print("Sorry, I couldn't understand your query or detect the top_n parameter.")
